package com.main;

import static com.util.Constant.*;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

public class GameFrame extends Frame {
    //    实例化GameBackGround类
    private GameBackGround gameBackGround;

    //    实例化bird类
    private Bird bird;

    //    实例化GameFrontGround类
    private GameFrontGround gameFrontGround;

    //    实例化障碍物类
    private GameBarrierLayer gameBarrierLayer;

    //    存放图片的图片
    private BufferedImage bufferimg = new BufferedImage(FRAM_WIDTH, FRAM_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);

    //    实例化GameOver类
    private GameOver gameOver;

    private Image ScreenImage = null;

    public GameFrame() {
//        是否可视化
        setVisible(true);
//        窗口的大小
        setSize(FRAM_WIDTH, FRAM_HEIGHT);
//        设置标题
        setTitle(FRAM_TITLE);
//        窗口初始化位置
        setLocation(FRAM_X, FRAM_Y);
//        窗口的大小不可改变
        setResizable(false);
//        窗口关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);  //结束程序
            }
        });

        initGame();
        new run().start();

//        添加按键监听
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                add(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                minu(e);
            }
        });
    }

    //    对游戏中的对象初始化
    public void initGame() {
        gameBackGround = new GameBackGround();
        bird = new Bird();
        gameFrontGround = new GameFrontGround();
        gameBarrierLayer = new GameBarrierLayer();
        gameOver = new GameOver();
    }


    class run extends Thread {
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(33);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     *需要的内容全在此方法中绘制
     */
    public void update(Graphics g) {
        Graphics graphics = bufferimg.getGraphics();
        if (bird.life) {
            //        得到图片的画笔
            gameBackGround.draw(graphics);
            bird.draw(graphics);
            gameFrontGround.draw(graphics);
            gameBarrierLayer.draw(graphics, bird);
//        一次性把图片绘制
            g.drawImage(bufferimg, 0, 0, null);
        } else {
            gameOver.draw(graphics);
//        一次性把图片绘制
            g.drawImage(bufferimg, 0, 0, null);
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", 1, 15));
            String reset = "Space Reset Game";
            g.drawString(reset, 225, 350);
        }
    }

    //    按键
    public void add(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                bird.fly(1);
                break;
            case KeyEvent.VK_SPACE:
                if (bird.life == false) {
                    restart();
                }
                break;
        }
    }

    public void minu(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                bird.fly(5);
                break;
        }
    }

    /*
     *重置游戏
     */
    public void restart() {
        gameBarrierLayer.restart();
        bird.restartDraw();
    }
}
